package StudentController;

import Student.Student;
import StudentService.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    public void start() {
        studentloop:
        while(true){
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            int s =sc.nextInt();
            switch (s){
                case 1:
                    addStudent();
                    break;
                case 2:
                    deleteStudentById();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    findAllStudent();
                    break;
                case 5:
                    System.out.println("感谢您使用学生管理系统，再见！");
                    break studentloop;
                default:
                    System.out.println("您的输入有误，请重新输入！");
                    break;
            }
        }
    }
    private StudentService studentService = new StudentService();
//这里要把private改成public方便调用

    //添加学生界面
    public void addStudent() {
        //键盘录入学生信息
        Scanner sc = new Scanner(System.in);
        String id;
        while(true){
            System.out.println("请输入学生id：");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if(flag)
            {
                System.out.println("学号已存在！请重新输入！");
            }
            else{
                break;
            }
        }
        //将学生信息封装
        Student stu =addStudentinfo(id);
        //将封装好的学生传给Service让他进行后续操作
         boolean result = studentService.addStudent(stu);
         if (result = true)
         {
             System.out.println("添加成功！");
         }
         else{
             System.out.println("添加失败！");
         }
    }


    //查询功能界面
    public void findAllStudent() {
        //1.指派业务员寻找需要的学生数组
        Student[] stus = studentService.findAllStudent();
        //2.判断是否为空
        if (stus == null){
            System.out.println("查无信息，请添加后重试");
            return ;
        }
        //3.如果成功返回进行打印
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if(stu!=null){
                System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t\t"+stu.getBrithday());
            }
        }
    }

    //删除功能界面
    public void deleteStudentById(){
        //输入学生id
        String delId = Judge();
        //通知业务员删除学生id
        studentService.deleteStudentById(delId);
        //提示删除成功
        System.out.println("删除成功！");
    }

    //修改功能界面
    public void updateStudent(){
        //输入学生id
        String updateId= Judge();
        Student newstu =addStudentinfo(updateId);
        //通知业务员StudentService修改学生信息
        studentService.updateStudent(updateId,newstu);
        //打印结果
        System.out.println("修改成功！");
    }
    //将修改信息会重复使用的代码封装
    public Student addStudentinfo(String id){
        System.out.println("请输入学生姓名：");
        String name = sc.next();
        System.out.println("请输入学生年龄：");
        String age = sc.next();
        System.out.println("请输入学生生日：");
        String birthday = sc.next();

        Student newstu =new Student();
        newstu.setId(id);
        newstu.setName(name);
        newstu.setAge(age);
        newstu.setBrithday(birthday);
        return newstu;
    }
    //将判断id是否存在重复使用的代码封装
    public String Judge(){
        String id;
        while(true){
            System.out.println("请输入学生id:");
            id = sc.next();
            //判断id是否存在
            boolean exists = studentService.isExists(id);
            if(!exists){
                System.out.println("您输入的id不存在，在请重新输入");
            }else{
                break;
            }
        }
        return id;
    }
}
